<?php

class Client_ProfileController extends App_Controller_Action
{	
	public function indexAction()
	{
		$request = $this->getRequest();
		$user_id = $this->getUserId();

		//check if it is from create service account
		$from  = $request->getParam('from');
		if ($from=='create'){
			$dpt_id  = $request->getParam('dpt_id');
			$area  = $request->getParam('area');
		}
		
		$form = new Form_ClientProfileForm();
		$this->view->form = $form;

		$model_client = new Model_UserClient();
		
		if ($this->getRequest()->isPost()) {

			$formData = $this->getRequest()->getPost();

			if ($form->isValid($formData)) {
				
				$formData['client_id'] = $user_id;
				$formData['email'] = $this->view->user_name;
				
				$result = $model_client->updateProfile($formData);
				
				if ($result){
					if ($from=='create' and !empty($dpt_id) ){
	 					$this->_redirect("/client/index/create-dpt-account/dpt_id/".$dpt_id."/area/".$area);						
					}
					
					$this->view->success_msg="Profile has been updated Successfully!";					
				}else{
					$this->view->error_msg="Sorry, Profile updated failed.";					
				}
				
			}
			
		}
		$profile = $model_client->getProfile($user_id);
		if (!empty($profile))
			$form->populate($profile);		
		
    }
		
}

